package setting

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/setting"
	"likeadmin/core"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var SmsGroup = core.Group("/setting/sms", newSmsHandler, regSms, middleware.TokenAuth())

func newSmsHandler(srv setting.ISmsSettingService) *smsHandler {
	return &smsHandler{srv: srv}
}

func regSms(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *smsHandler) {
		rg.GET("/list", handle.list)
		rg.GET("/detail", handle.detail)
		rg.POST("/save", handle.edit)

	})
}

type smsHandler struct {
	srv setting.ISmsSettingService
}

// list sms列表
func (hd smsHandler) list(c *gin.Context) {
	res, err := hd.srv.List()
	response.CheckAndRespWithData(c, res, err)
}

// detail setting详情
func (hd smsHandler) detail(c *gin.Context) {
	var detailReq req.SmsSettingDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(c, detailReq.Name)

	response.CheckAndRespWithData(c, res, err)
}

// edit setting编辑
func (hd smsHandler) edit(c *gin.Context) {
	var editReq req.SmsSettingEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Edit(c, editReq))
}
